package nl.infodation.emr.pricingservice.controller;

import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.data.ListBoxData;
import nl.infodation.emr.pricingservice.entity.LanguageSystem;
import nl.infodation.emr.pricingservice.entity.Role;
import nl.infodation.emr.pricingservice.entity.SystemValue;
import nl.infodation.emr.pricingservice.entity.SystemValue.TableName;
import nl.infodation.emr.pricingservice.entity.SystemValueRef;
import nl.infodation.emr.pricingservice.entity.SystemValueRole;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author SUCCESS\trule create:22/08/2013
 */
public class ListBoxDataController {
	protected static final Logger LOG = LoggerFactory
			.getLogger(ListBoxDataController.class);

	private Role getRole(String roleName) {
		if (StringUtil.isEmptyOrBlank(roleName)) {
			return null;
		}
		
		Role searchItem = new Role();
		searchItem.setRoleName(roleName);
		return ControllerFactory.getRoleController().findFirstByEntity(searchItem);
	}

	private List<SystemValueRole> getSystemValueRoles(final String tableName, String roleName) {
		List<SystemValueRole> systemValueRoles = new ArrayList<SystemValueRole>();
		final Role role = this.getRole(roleName);

		if (null == role || role.getRoleId() == 0) {
			LOG.error("Cannot found role");
		} else {
			final SystemValue subSearchItem = new SystemValue();
			subSearchItem.setSystemName(TableName.parse(tableName));
			
			final SystemValueRole searchItem = new SystemValueRole();
			searchItem.setRole(role);
			searchItem.setActived(Boolean.TRUE);
			searchItem.setSystemValue(subSearchItem);
			systemValueRoles = ControllerFactory.getSystemValueRoleController().findByEntity(searchItem);
		}
		return systemValueRoles;
	}

	private List<SystemValue> getSystemValuesFrom(final List<SystemValueRole> systemValueRoles) {
		final List<SystemValue> systemValues = new ArrayList<SystemValue>();
		for (SystemValueRole systemValueRole : systemValueRoles) {
			if (null != systemValueRole.getSystemValue()) {
				systemValues.add(systemValueRole.getSystemValue());
			}
		}
		return systemValues;
	}
	
	private List<SystemValueRef> getSystemValueRef(final SystemValue systemValue,final LanguageSystem languageSystem) {
		final SystemValueRef searchItem = new SystemValueRef();
		searchItem.setLanguageSystem(languageSystem);
		searchItem.setSystemValue(systemValue);
		return ControllerFactory.getSystemValueRefController().findByEntity(searchItem);
	}
	
	
	/**
	 * get items of list box with the table name 
	 * @param tableName
	 * @return List<ListBoxData>
	 */
	public List<ListBoxData> getSystemValueRefs(final String tableName, String roleName) {
		final List<SystemValueRole> systemValueRoles = this.getSystemValueRoles(tableName, roleName);
		final List<SystemValue> systemValues = getSystemValuesFrom(systemValueRoles);
		final Integer languageId = SessionData.getLanguage();
		final LanguageSystem languageSystem = ControllerFactory.getLanguageSystemController().getObjectById(languageId);
		
		final List<ListBoxData> boxDatas = new ArrayList<ListBoxData>();

		if (null == systemValues || systemValues.isEmpty()) {
			LOG.error("Cant not found system value");
		} else {
			final List<SystemValueRef> systemValueRefs = new ArrayList<SystemValueRef>();
			for (SystemValue systemValue : systemValues) {
				systemValueRefs.addAll(getSystemValueRef(systemValue, languageSystem));
			}
			if (systemValueRefs != null && !systemValueRefs.isEmpty()) {
				for (SystemValueRef systemValueRef : systemValueRefs) {
					boxDatas.add(new ListBoxData(systemValueRef.getSystemValue().getSystemCode(), systemValueRef.getLabel()));
				}
			}
		}
		return boxDatas;
	}
	
	public ListBoxData getKeyFromPopup(final String key, final Enum<TableName> type) {
		final Integer languageId = SessionData.getLanguage();
		final SystemValue systemValue = new SystemValue();
		final LanguageSystem languageSystem = new LanguageSystem();
		ListBoxData boxData = null;

		systemValue.setSystemCode(key);
		systemValue.setSystemName((TableName) type);
		languageSystem.setLanguageSystemId(languageId);

		final List<SystemValueRef> systemValueRefs = this.getSystemValueRef(
				systemValue, languageSystem);
		if (systemValueRefs != null && systemValueRefs.size()>0) {
			boxData = new ListBoxData(systemValueRefs.get(0).getSystemValue().getSystemCode(), systemValueRefs.get(0).getLabel());
		}

		return boxData;
	}

}
